<template>
  <div class="common-layout">
    <el-row class="min-h-10 h-screen">
      <el-col :lg="16" class="flex justify-center items-center bg-blue-400" style="display: flex;">
        <div>
          <div class="font-bold text-6xl text-white">高考数据分析系统</div>
          <div>我们将提供最好的服务</div>
        </div>
      </el-col>
      <el-col :lg="8" class="flex items-center justify-center bg-gray-100" style="display: flex;">
        <div>
          <h2 class="text-center font-bold text-3xl pb-10">登录</h2>
          <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form" label-width="auto" style="max-width: 600px">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="请输入用户名">
                <template #prefix>
                  <el-icon><User /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="请输入密码" @keyup.enter="handleLogin" show-password>
                <template #prefix>
                  <el-icon><Lock /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter="handleLogin">
                <template #prefix>
                  <el-icon><Check /></el-icon>
                </template>
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
            <el-form-item style="width:100%;">
              <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
                <span v-if="!loading">登录</span>
                <span v-else>登录中...</span>
              </el-button>
              <router-link to="/register">
                <el-button class="block" style="margin-top: 10px;">注册</el-button>
              </router-link>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <!-- 底部 -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import { ElNotification, ElMessageBox } from 'element-plus';
import { getCodeImg } from '@/api/login';
import { encrypt, decrypt } from '@/utils/jsencrypt';
import useUserStore from '@/store/modules/user';
import { User, Lock, Check } from '@element-plus/icons-vue';

const userStore = useUserStore();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = reactive({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: ""
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const codeUrl = ref("");
const loading = ref(false);
const captchaEnabled = ref(true);

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      if (loginForm.rememberMe) {
        localStorage.setItem("username", loginForm.username);
        localStorage.setItem("password", encrypt(loginForm.password));
        localStorage.setItem("rememberMe", loginForm.rememberMe);
      } else {
        localStorage.removeItem("username");
        localStorage.removeItem("password");
        localStorage.removeItem("rememberMe");
      }
      userStore.login(loginForm).then(() => {
        router.push({ path: "/" });
      }).catch(err => {
        loading.value = false;
        if (captchaEnabled.value) {
          getCode();
        }
        ElNotification({
          title: 'Warning',
          message: err.response.data.msg,
          type: 'warning',
        });
      });
    }
  });
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.uuid = res.uuid;
    }
  });
}

function getCookie() {
  const username = localStorage.getItem("username");
  const password = localStorage.getItem("password");
  const rememberMe = localStorage.getItem("rememberMe");
  loginForm.username = username || loginForm.username;
  loginForm.password = password ? decrypt(password) : loginForm.password;
  loginForm.rememberMe = rememberMe ? Boolean(rememberMe) : false;
}

getCode();
getCookie();
</script>

<style scoped>
.common-layout {
  height: 100%;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
